/*
 * This program is free software: you can redistribute it and/or modify it under
 * the terms of the GNU General Public License as published by the Free Software
 * Foundation, either version 3 of the License, or (at your option) any later
 * version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
 * details.
 *
 * You should have received a copy of the GNU General Public License along with
 * this program. If not, see <http://www.gnu.org/licenses/>.
 */
package org.merak.core.persistence.jdbc.systems;

import java.io.File;
import java.io.FileNotFoundException;

import org.merak.core.Application;
import org.merak.core.model.SettingException;
import org.merak.core.persistence.jdbc.DatabaseSystem;
import org.merak.core.persistence.jdbc.settings.ConnectionDetail;

public class MsAccess extends DatabaseSystem {

	//~ Attributes ///////////////////////////////////////////////////////////////
	//****************************************************************************
	private final String  standardDriver = "sun.jdbc.odbc.JdbcOdbcDriver";
	private final Integer standardPort   = null;

	//~ Methods //////////////////////////////////////////////////////////////////
	//****************************************************************************
	@Override
	public String getStandardDriver() {
		return this.standardDriver;
	}
	//****************************************************************************
	@Override
	public Integer getStandardPort() {
		return this.standardPort;
	}
	//****************************************************************************
	@Override
	public String formatUrl(ConnectionDetail s) throws Exception {

		// Validate
		if (s.getDriverType() == null) throw new SettingException(
			"Database DriverType is missing"
		);

		/* If the MsAccess file don't have an Absolute Path, search the file
		 * in the ApplicationData directory by default. */
		File database = new File(s.getDatabase());
		if ( !database.isAbsolute() ) {
			database = Application.paths.applicationData.getFile(s.getDatabase());
		}

		// Check if the file exists
		if ( ! database.exists() ) throw new FileNotFoundException (
				"The MsAccess file does not exist: " + database
		);

		// Format the url pointing to the file
		return "jdbc:odbc:Driver={" + s.getDriverType() + "};DBQ=" + s.getDatabase();

	}
	//****************************************************************************

}

